/*
 * ICS 314 Avionics Assignment
 * @author ICS 314 Team Haines {Kevon Adonis, Alex Char, David Teroaka, Samuel Roberts}
 * @copyright 2014
 */

package avionics;

/**
Last revision - 2014-05-05 by Alex

This class is responsible for calculating the deflection of the needle in order to drive the GUI
interface for the VOR.

It takes the radial from the radio station (see AvionicsRadioSimulator.java) which is an integer
value from 0 to 359.

In order to actually calculate if the plane needs to turn left or right, this means we need to know
what direction the plane is pointing???  Based on how little the device is supposed to know I'm
not sure about this.
^
Actually, there IS an input from the pilot: the desired input from the OBS controller.  The pilot
says he wants a heading to 270 degrees, and then based on what the pilot inputs and the radial
broadcast by the radio station the direction needs to be adjusted.  FIGURE OUT WHAT POSSIBLE VALUES
CAN BE INPUT BY THE PILOT INTO THE OBS.

But once the DeflectionCalc spits out "LEFT" or "RIGHT", then what happens when the aircraft turns
left or right?  Does the needle swing left and right?

FROM THE ASSIGNMENT:
We are assumed the aircraft is at least "kind of" pointed towards the radio station, within 90
degrees.

From Wikipedia:

Looking at the picture to the right, you can see that the needle is centered and the instrument gives
a FROM indication (as a small white triangle is registered and pointing to "FR"); So the aircraft is
on the 254 degree radial. It doesn't matter what the heading of the aircraft is; it is located somewhere
along a line 254 from the VOR station. In order to fly to the VOR station, you would first twist the
OBS knob until the needle is centered and the white triangle registers, pointing next to the "TO"
designator (in the opposite direction, or up, from the "FR" designator). Note that this will be 74
degrees, exactly 180 from the current radial. Now turn the aircraft to this new heading and keep the
needle centered -- this will take you to the VOR station.

From Laulima discussion page:
I have just been thinking about it, so far 00 and 3600 seem to work well interchangeably as input.
For instance, if you want to head towards 1800, you would then be traveling from the reciprocal radial.
While you are on the TO side, the reciprocal of 180 is what you would intercept with correct navigation.
I used the method of + or - 180 for Reciprocal i.e if>180 subtract 180, if <180 add 180.
The reciprocal of 180 in this case is either 180+180 or 180-180 which leads to the same point 00/3600 .
The TO side for 0 would be between to (0+90 and 0-90)0 mod 360, the TO side for 360 would be between
(360+90 and 360-90)0mod 360. Considering mod 360, these two define the same area.
*/

public class DeflectionCalc  {

    /**
    Determines the angle of deflection given the radio's angle and the OBS angle.  This value can
    be a negative value.

    radioRadial - a value from 0 to 359, probably received from AvionicsRadioSimulator.java
    obsRadial - a value from 0 - 359, selected by the pilot (user)

    UNCERTAIN:
    If the value is negative then it means the airplane has passed the station and is flying away
    from it.

    UNCERTAIN appears to be false; there are several conditions where the value can turn negative
    and do not appear to be related to the plane's position to the radio station in a predictable
    manner.
    */
    public int getDeflection(int radioRadial, int obsRadial)  {
        int deflection = radioRadial - obsRadial;

        return deflection;
    }

    /**
    Based on the value of the two given radials, determines whether the airplane is flying towards
    the station, has already passed the station, or is in a location where the plane is unable to
    see the station.  Examples of unable to see are when the plane is at a 90 or 270 degree angle,
    known as "abeam" of the station.

    radioRadial - a value from 0 to 359, probably received from AvionicsRadioSimulator.java
    obsRadial - a value from 0 - 359, selected by the pilot (user)

    <Possible flags>
    TO   - plane is headed towards the radio station
    FROM - plane has past the radio station
    OFF  - plane is abeam of the station (at 90 or 270 degrees to the station)
    */

    public String getFlag(int radioRadial, int obsRadial)  {
        String flag = "";

        //start with the most obvious cases, then work downwards
        //radio station is dead ahead, and OBS is tuned directly at it
        if(radioRadial == 180 && obsRadial == 0)  {
            flag = "TO";
        }
        //radio station is straight behind, and OBS is tuned straight ahead
        else if(radioRadial == 0 && obsRadial == 0)  {
            flag = "FROM";
        }
        //error case if none of the above conditions are met
        //remove this case when thoroughly tested
        else  {
            flag = "ERROR";
        }

        return flag;
    }

} //end of class